<?php

namespace Lib;

require_once "injectable.php";

abstract class Injector implements Injectable {
    protected $_injectionInstances=[];
    
    public function ___inject($property, $value) {
        $this->_injectionInstances[$property] = $value;
    }

    public function __get($name) {
        $di = di();
        $dependencies = $di->getDependencies(static::class);

        if(isset($this->_injectionInstances[$name])) {
            return $this->_injectionInstances[$name];
        }

        if($dependencies[$name]) {
            $this->___inject($name, $di->get($dependencies[$name]['class']));
        }

        return $this->_injectionInstances[$name];
    }

    public function __set($name, $value) {
        $di = di();
        $dependencies = $di->getDependencies(static::class);

        if($dependencies[$name] && $dependencies[$name]['write']) {
            $this->___inject($name, $di->get($dependencies[$name]['class']));
        }
    }
}